package command
{
   import com.greensock.TweenMax;
   import com.greensock.easing.Linear;
   import com.greensock.events.TweenEvent;
   import com.greensock.plugins.FramePlugin;
   import com.greensock.plugins.TweenPlugin;
   
   import flash.display.FrameLabel;
   import flash.display.MovieClip;
   import flash.events.Event;
   
   import jp.progression.commands.Command;
   
   /**
    * @author cjboy | cjboy1984@gmail.com
    * @date   Oct,15,2010
    * @example
    * addCommand(
    *    new DoLabelTween(mc, "stop", { onUpdate:update, onComplete:complete } )
    * );
    */
   public class DoLabelTween extends Command
   {
      private var tween:TweenMax;
      private var target:MovieClip;
      private var target_label:String;
      private var target_frame:int;
      private var parameters:Object;
      
      private var frameRate:Number;
      
      public function DoLabelTween(tar:MovieClip, label:String, param:Object = null, initObj:Object = null)
      {
         if (tar.stage)
         {
            // activate GreenSock plugin
            TweenPlugin.activate([FramePlugin]);
            
            target = tar;
            target_label = label;
            target_frame = getFrameByLabel(tar, target_label);
            parameters = param || {};
            
            frameRate = tar.stage.frameRate;
            
            // save frame property
            if (parameters)
            {
               parameters.frame = target_frame;
               parameters.ease = Linear.easeNone;
            }
            
            super(myExecute, myInterrupt, initObj);
         }
      }
      
      // ################### protected ##################
      
      override public function executeComplete():void
      {
         super.executeComplete();
      }
      
      override public function clone():Command
      {
         return new DoLabelTween(target, target_label, parameters);
      }
      
      // #################### private ###################
      
      private function myExecute():void
      {
         var dur:Number = Math.abs(target_frame - target.currentFrame) / frameRate;
         
         tween = new TweenMax(target, dur, parameters);
         tween.addEventListener(TweenEvent.COMPLETE, myComplete, false, int.MAX_VALUE, true);
         
         if (tween)
         {
            return;
         }
         else
         {
            executeComplete();
         }
      }
      
      private function myComplete(e:Event = null):void
      {
         tween.removeEventListener(TweenEvent.COMPLETE, myComplete, false);
         // override executeComplete();
         executeComplete();
      }
      
      // TODO: figure out progression interrupt()
      private function myInterrupt():void
      {
         tween.kill();
         interrupt();
      }
      
      // --------------------- LINE ---------------------
      
      private function getFrameByLabel(mc:MovieClip, label:String):int
      {
         if (!mc) return 1;
         
         var ret:int = 1;
         
         for (var i:int = 0; i < mc.currentLabels.length; ++i)
         {
            var lb:FrameLabel = mc.currentLabels[i] as FrameLabel;
            if (lb.name == label)
            {
               ret = lb.frame;
               break;
            }
         }
         
         return ret;
      }
      
      // --------------------- LINE ---------------------
      
   }
   
}